package com.baizhi.controller.convert;

import org.springframework.core.convert.converter.Converter;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class LocalDateTimeConverter implements Converter<String, LocalDateTime> {
    private static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    @Override
    public LocalDateTime convert(String source) {
        if (source.trim().length() == 0) {
            return null;
        }
        // 先尝试ISO格式: 2019-07-15T16:00:00
        try {
            return LocalDateTime.parse(source);
        } catch (Exception e) {
            return LocalDateTime.parse(source, DateTimeFormatter.ofPattern(DATE_TIME_PATTERN));
        }
    }
}
